<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="用户名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Back" @click="handleBack"
          >返回</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAuthUserAdd"
          v-hasPermi="['sys:role:add']"
          >添加用户
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="CircleClose"
          :disabled="multiple"
          @click="handleAuthUserRemove"
          v-hasPermi="['sys:role:delete']"
          >取消授权</el-button
        >
      </el-col>
      <y-right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      ref="userRef"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="用户名称"
        prop="username"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="用户昵称"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="邮箱"
        prop="email"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="手机"
        prop="mobile"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <y-dict-tag :options="sys_enable_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="create_time"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="CircleClose"
            @click="handleAuthUserRemove(scope.row)"
            v-hasPermi="['sys:role:delete']"
            >取消授权</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <y-pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />
    <select-user
      ref="selectRef"
      :roleId="queryParams.roleId"
      @ok="handleQuery"
    />
  </div>
</template>

<script setup name="AuthUser">
import selectUser from "./selectUser";
import { relatedUserList, authUserRemove } from "@/api/sys/role";

const route = useRoute();
const { proxy } = getCurrentInstance();
const { sys_enable_disable } = proxy.useDict("sys_enable_disable");

const userList = ref([]);
const loading = ref(true);
const showSearch = ref(false);
const multiple = ref(true);
const total = ref(0);
const userIds = ref([]);
const userNames = ref([]);

const queryParams = reactive({
  roleId: route.params.roleId,
  username: undefined,
  mobile: undefined,
  page: 1,
  limit: 10,
});

/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  relatedUserList(queryParams).then((response) => {
    userList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}

// 返回按钮
function handleBack() {
  proxy.$router.back();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  userIds.value = selection.map((item) => item.id);
  userNames.value = selection.map((item) => item.name);
  multiple.value = !selection.length;
}

/** 打开授权用户表弹窗 */
function handleAuthUserAdd() {
  proxy.$refs["selectRef"].show();
}

/** 取消授权按钮操作 */
function handleAuthUserRemove(row) {
  userIds.value = row.id ? [row.id] : userIds.value;
  userNames.value = row.name ? row.name : userNames.value;
  proxy.$modal
    .confirm('确认要取消用户"' + userNames.value + '"的授权吗？')
    .then(function () {
      return authUserRemove({
        roleId: queryParams.roleId,
        userIds: userIds.value,
      });
    })
    .then(() => {
      proxy.$modal.msgSuccess("取消授权成功");
      getList();
    })
    .catch(() => {});
}

onMounted(() => {
  getList();
});
</script>
